//
//  AlertTools.m
//  Azkar
//
//  Created by 王易 on 2018/8/25.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "AlertTools.h"
#define Delay 1.5

/**
 HUD工具
 */
@implementation AlertTools

+ (MBProgressHUD*)showMessage:(NSString*)message
{
    UIView *rootView = [self currtView];
    MBProgressHUD *hud = [MBProgressHUD HUDForView:rootView];
    if (!hud) {
        hud = [MBProgressHUD showHUDAddedTo:rootView animated:YES];
    }
    
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    hud.label.numberOfLines = 0;
    hud.label.textColor = [UIColor whiteColor];
    [hud hideAnimated:YES afterDelay:Delay];
    return hud;
}

+ (MBProgressHUD*)showLoading
{
    UIView *rootView = [self currtView];
    MBProgressHUD *hud = [MBProgressHUD HUDForView:rootView];
    if (!hud) {
        hud = [MBProgressHUD showHUDAddedTo:rootView animated:YES];
    }
    
    hud.removeFromSuperViewOnHide = YES;
    [rootView addSubview:hud];
    [hud showAnimated:YES];
    return hud;
}

+ (void)hideLoading
{
    UIView *rootView = [self currtView];
    MBProgressHUD *hud = [MBProgressHUD HUDForView:rootView];
    [hud hideAnimated:YES];
}
+ (UIView *)currtView
{
    
    NSInteger count = [UIApplication sharedApplication].windows.count;
    UIView *currtView;
    for (NSInteger i = count -1; i<count; i--) {
        UIWindow *window = [UIApplication sharedApplication].windows[i];
        if (window.hidden == NO) {
            currtView = window;
            break;
        }
    }
    return currtView;
}
@end
